package contacts.view;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import contacts.controller.ContactsControllerListener;
import contacts.dao.impl.RoomDaoImpl;
import contacts.po.Student;

public class ContactsDialog extends JDialog {
	private static final long serialVersionUID = 1L;
	private ContactsControllerListener contactsControllerListener;
	private JTextField studentIdTextField, studentNameTextField, birthdayTextField, phoneTextField, emailTextField,
			addressTextField, roomNoTextField;
	private JButton confirmButton;
	private JPanel studentPanel;
	private Student student;

	public ContactsDialog(ContactsFrame owner, String title, Student student) {
		this.contactsControllerListener = owner.getControllerListener();
		this.contactsControllerListener.setStudentDialog(this);
		this.setTitle(title);
		this.student = student;
		this.add(getStudentPanel());
		this.setModal(true);

		this.setLocationRelativeTo(null);
		this.setVisible(true);
		this.validate();
	}

	public void closeCurrentDialog() {
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
	}

	/**
	 * 增删改面板
	 */
	private JPanel getStudentPanel() {
		studentPanel = new JPanel();
		if ("添加学生信息".equals(this.getTitle())) {
			studentIdTextField = new JTextField(15);
			studentNameTextField = new JTextField(15);
			birthdayTextField = new JTextField(15);
			phoneTextField = new JTextField(15);
			emailTextField = new JTextField(15);
			addressTextField = new JTextField(15);
			roomNoTextField = new JTextField(15);
			birthdayTextField.setText("xxxx-xx-xx");
			birthdayTextField.addFocusListener(contactsControllerListener);
			confirmButton = new JButton("确定添加");
			confirmButton.setActionCommand("addPost");
			confirmButton.addActionListener(contactsControllerListener);
			studentPanel.add(new JLabel("学号        "));
			studentPanel.add(studentIdTextField);
			studentPanel.add(new JLabel("姓名        "));
			studentPanel.add(studentNameTextField);
			studentPanel.add(new JLabel("生日        "));
			studentPanel.add(birthdayTextField);
			studentPanel.add(new JLabel("电话        "));
			studentPanel.add(phoneTextField);
			studentPanel.add(new JLabel("邮箱        "));
			studentPanel.add(emailTextField);
			studentPanel.add(new JLabel("家庭地址"));
			studentPanel.add(addressTextField);
			studentPanel.add(new JLabel("宿舍号     "));
			studentPanel.add(roomNoTextField);
			studentPanel.add(confirmButton);
			this.setSize(270, 270);
		} else if ("修改学生信息".equals(this.getTitle())) {
			studentIdTextField = new JTextField(student.getStudentId(), 15);
			studentNameTextField = new JTextField(student.getStudentName(), 15);
			birthdayTextField = new JTextField(student.getBirthday(), 15);
			phoneTextField = new JTextField(student.getPhone(), 15);
			emailTextField = new JTextField(student.getEmail(), 15);
			addressTextField = new JTextField(student.getAddress(), 15);
			roomNoTextField = new JTextField(new RoomDaoImpl().getRoomNo(student.getRoom()), 15);
			studentIdTextField.setEditable(false);
			confirmButton = new JButton("确定修改");
			confirmButton.setActionCommand("updatePost");
			confirmButton.addActionListener(contactsControllerListener);
			studentPanel.add(new JLabel("学号        "));
			studentPanel.add(studentIdTextField);
			studentPanel.add(new JLabel("姓名        "));
			studentPanel.add(studentNameTextField);
			studentPanel.add(new JLabel("生日        "));
			studentPanel.add(birthdayTextField);
			studentPanel.add(new JLabel("电话        "));
			studentPanel.add(phoneTextField);
			studentPanel.add(new JLabel("邮箱        "));
			studentPanel.add(emailTextField);
			studentPanel.add(new JLabel("家庭地址"));
			studentPanel.add(addressTextField);
			studentPanel.add(new JLabel("宿舍号     "));
			studentPanel.add(roomNoTextField);
			studentPanel.add(confirmButton);
			this.setSize(270, 270);
		} else if ("删除学生信息".equals(this.getTitle())) {
			studentIdTextField = new JTextField(student.getStudentId(), 15);
			studentIdTextField.setEditable(false);
			confirmButton = new JButton("确定删除");
			confirmButton.setActionCommand("deletePost");
			confirmButton.addActionListener(contactsControllerListener);
			studentPanel.add(new JLabel("学号"));
			studentPanel.add(studentIdTextField);
			studentPanel.add(confirmButton);
			this.setSize(250, 120);
		}
		return studentPanel;
	}

	public JTextField getStudentIdTextField() {
		return studentIdTextField;
	}

	public JTextField getStudentNameTextField() {
		return studentNameTextField;
	}

	public JTextField getBirthdayTextField() {
		return birthdayTextField;
	}

	public JTextField getPhoneTextField() {
		return phoneTextField;
	}

	public JTextField getEmailTextField() {
		return emailTextField;
	}

	public JTextField getAddressTextField() {
		return addressTextField;
	}

	public JTextField getRoomNoTextField() {
		return roomNoTextField;
	}

	public JButton getConfirmButton() {
		return confirmButton;
	}

}
